<template>
  <div id="admin-manage" class="page-wrapper">
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <GroupSelector :selectorData="selectorData" />
      </div>
      <el-button type="primary" @click="handleSubmitClick">获取数据</el-button>
    </div>

    <div class="wrapper main-card">
      <el-table
        ref="singleTable"
        :data="pageData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column property="number" label="账户名"></el-table-column>
        <el-table-column
          property="meterNumber"
          label="联系方式"
        ></el-table-column>
        <!-- <el-table-column property="name" label="房间类型" width="120"></el-table-column> -->
        <el-table-column
          property="totalMoney"
          label="操作权限"
        ></el-table-column>

        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              @change="handleSwitchChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column property="date" label="上次登录时间"></el-table-column>

        <el-table-column property="button" label="操作">
          <template slot-scope="scope">
            <el-button @click="modify(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="参数详情"
      :visible.sync="dialogVisible"
      width="800px"
      height="800px"
    >
      <el-tabs type="border-card">
        <el-form class="form" ref="form" :model="modifydata">
          <div class="form-item-wrapper">
            <el-form-item label="电表编号" required prop="meterNumber">
              <el-input v-model.trim="modifydata.meterNumber"></el-input>
            </el-form-item>
            <el-form-item label="主控编号" required prop="master_id">
              <el-input
                v-model.trim="modifydata.master_id"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="使用功率" required prop="power">
              <el-input
                v-model.trim="modifydata.power"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
          <div class="form-item-wrapper">
            <el-form-item label="限制功率" required prop="limitPower">
              <el-input
                v-model.trim="modifydata.limitPower"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="存款余额" required prop="depositMoney">
              <el-input
                v-model.trim="modifydata.depositMoney"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
            <el-form-item label="补助金额" required prop="subsidyMoney">
              <el-input
                v-model.trim="modifydata.subsidyMoney"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import GroupSelector from '@/components/GroupSelector'
import { getmeter, upmeter, getAllmeter } from '@/api/meter'
export default {
  name: 'RoomInfo',
  components: {
    GroupSelector
  },
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      dialogVisible: false,
      selectorData: {
        buildingId: null,
        floorId: null,
        roomId: null
      },
      formData: {},
      modifydata: {},
      tableData: [],
      currentRow: null
    }
  },

  methods: {
    // 实现分页的方法
    queryByPage() {
      // 起始位置 = (当前页 - 1) x 每页的大小
      const start = (this.currentPage - 1) * this.pageSize
      // 结束位置 = 当前页 x 每页的大小
      const end = this.currentPage * this.pageSize

      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val

      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val

      this.pageData = this.queryByPage()
    },

    handleSwitchChange(row) {
      console.log(row)
      upmeter(row).then(res => {
        console.log(res.data.result.state)
        if (res.data.result.state === '1') {
          this.$message.success('开关已打开')
        } else {
          this.$message.warning('开关已关闭')
        }
      })
    },
    //搜索按钮
    handleSubmitClick() {
      getAllmeter(this.selectorData).then(res => {
        console.log(res)
        if (res.success == true) {
          this.tableData = res.data
          this.pageData = res.data
          this.total = res.data.length

          console.log(this.tableData)
        }
      })
    },
    modify(row) {
      console.log(row)
      this.modifydata = row
      this.dialogVisible = true
    },
    handleSubmit() {
      upmeter(this.modifydata).then(res => {
        console.log(res)
        if (res.success == true) {
          this.$message.success('修改成功')
          this.dialogVisible = false
        }
      })
    }
  },

  mounted() {
    getmeter().then(res => {
      this.total = res.data.meter.length
      if (res.success == true) {
        this.tableData = res.data.meter
        this.pageData = this.queryByPage()
      }
    })
  }
}
</script>
<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}

.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center; /* 垂直居中 */
}

.list-wrapper {
  margin-top: 40px;
}
.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}
.form-item-wrapper {
  display: flex;
  justify-content: space-between;
}
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px; /* Adjust margin as needed */
}
</style>
